package com.lin.mapper;

import com.lin.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("insert into user(username,password,addr,birthday,sex,email,status,money)" +
            "values(#{username},#{password},#{addr},#{birthday},#{sex},#{email},#{status},#{money})")
    boolean add(User user);

    @Delete("delete from user where uid = #{uid}")
    boolean delete(Integer uid);

    @Update("UPDATE user SET " +
            "username = #{username}, " +
            "password = #{password}, " +
            "addr = #{addr}, " +
            "birthday = #{birthday}, " +
            "sex = #{sex}, " +
            "email = #{email}, " +
            "status = #{status}, " +
            "money = #{money} " +
            "WHERE uid = #{uid}")
    boolean update(User user);

    @Select("select uid,username,password,addr,birthday,sex," +
            "TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE()) AS age,email,status,money " +
            "from user where uid = #{uid}")
    User queryById(Integer uid);

    @Select("select * from user")
    List<User> queryAll();

    @Update("update user set money = money - #{money} where name = #{from}")
    boolean transferOut(@Param("from") String from, @Param("to") String to, @Param("money") Integer money);

    @Update("update user set money = money + #{money} where name = #{from}")
    boolean transferIn(@Param("from") String from, @Param("to") String to, @Param("money") Integer money);
}
